package org.linlinjava.litemall.db.domain;

import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * litemall商品
 *
 * @author yliyun
 * @date 2022/10/28
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("litemall_merchandise")
public class LitemallMerchandise {
    /**
     * 被删除
     */
    public static final Boolean IS_DELETED = Deleted.IS_DELETED.value();

    /**
     * 不删除
     */
    public static final Boolean NOT_DELETED = Deleted.NOT_DELETED.value();

    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 商店id
     */
    private Integer shopId;


    /**
     * 商店名字
     */
    @TableField(exist = false)
    private String shopName;

    private String classification;

    private String label;

    private String brand;

    /**
     * 商品sn
     */
    private String merchandiseSn;

    /**
     * 名字
     */
    private String name;

    /**
     * 照片url
     */
    private String picUrl;

    /**
     * 销售价格
     */
    private BigDecimal sellingPrice;

    /**
     * 购买价格
     */
    private BigDecimal purchasePrice;

    /**
     * 数量
     */
    private Double number;

    /**
     * 基本单位
     */
    private String basicUnit;

    /**
     * 检查单位
     */
    private String checkUnit;

    /**
     * 单位
     */
    private String unit;

    /**
     * 检查率
     */
    private Integer checkRatio;

    /**
     * 比
     */
    private Integer ratio;

    /**
     * 模型
     */
    private String model;

    /**
     * 标准
     */
    private String standard;

    /**
     * 状态
     */
    private Byte status;

    /**
     * 总部库存
     */
    private Double hqNumber;

    private String property;
    /**
     * 删除
     */
    @TableLogic
    private Boolean deleted;

    @TableField(fill = FieldFill.INSERT) //插入时自动填充
    private LocalDateTime addTime;

    @TableField(fill = FieldFill.UPDATE) // 插入 和 更新时 自动填充
    private LocalDateTime updateTime;

    /**
     * 添加用户id
     */
    private Integer addUserId;

    /**
     * 更新用户id
     */
    private Integer updateUserId;

    //判断是否原料
    @TableField(exist = false)
    private Boolean isRrc;

    public enum Deleted {
        NOT_DELETED(new Boolean("0"), "未删除"),
        IS_DELETED(new Boolean("1"), "已删除");

        private final Boolean value;

        private final String name;

        Deleted(Boolean value, String name) {
            this.value = value;
            this.name = name;
        }

        public Boolean getValue() {
            return this.value;
        }

        public Boolean value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }
    }

    public enum Column {
        id("id", "id", "INTEGER", false),
        shopId("shop_id", "shopId", "INTEGER", false),
        merchandiseSn("merchandise_sn", "merchandiseSn", "VARCHAR", false),
        name("name", "name", "VARCHAR", true),
        picUrl("pic_url", "picUrl", "VARCHAR", false),
        sellingPrice("selling_price", "sellingPrice", "DECIMAL", false),
        purchasePrice("purchase_price", "purchasePrice", "DECIMAL", false),
        number("number", "number", "INTEGER", true),
        basicUnit("basic_unit", "basicUnit", "VARCHAR", false),
        checkUnit("check_unit", "checkUnit", "VARCHAR", false),
        unit("unit", "unit", "VARCHAR", false),
        checkRatio("check_ratio", "checkRatio", "INTEGER", false),
        ratio("ratio", "ratio", "INTEGER", false),
        model("model", "model", "VARCHAR", false),
        standard("standard", "standard", "VARCHAR", true),
        status("status", "status", "TINYINT", true),
        deleted("deleted", "deleted", "BIT", false),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        addUserId("add_user_id", "addUserId", "INTEGER", false),
        updateUserId("update_user_id", "updateUserId", "INTEGER", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        public String value() {
            return this.column;
        }

        public String getValue() {
            return this.column;
        }

        public String getJavaProperty() {
            return this.javaProperty;
        }

        public String getJdbcType() {
            return this.jdbcType;
        }

        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        public static Column[] excludes(Column ... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
